<template>
  <div class="index">
    <div class="container">
      <!-- 轮播 start -->
      <div class="swiper-box">
        <div class="nav-menu">
          <ul class="menu-wrap">
            <li class="menu-item">
              <a href="javascript:;">手机 电话卡</a>
              <div class="children">
                <ul v-for="(item, i) in menuList" :key="i">
                  <li v-for="(sub, j) in item" :key="j">
                    <a :href="sub ? '/#/product/' + sub.id : ''">
                      <img
                        :src="
                          sub
                            ? sub.img
                            : require('../../public/imgs/item-box-1.png')
                        "
                        alt=""
                      />
                      {{ sub ? sub.name : "小米9" }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">电视 盒子</a>
            </li>
            <li class="menu-item">
              <a href="javascript:;">笔记本 平板</a>
            </li>
            <li class="menu-item">
              <a href="javascript:;">家电 插线板</a>
            </li>
            <li class="menu-item">
              <a href="javascript:;">出行 穿戴</a>
            </li>
            <li class="menu-item">
              <a href="javascript:;">智能 路由器</a>
            </li>
            <li class="menu-item">
              <a href="javascript:;">电源 配件</a>
            </li>
            <li class="menu-item">
              <a href="javascript:;">生活 箱包</a>
            </li>
          </ul>
        </div>
        <swiper :options="swiperOption">
          <swiper-slide v-for="item in sliderList" :key="item.id">
            <a :href="'/#/product/' + item.id">
              <img :src="item.img" />
            </a>
          </swiper-slide>
          <!-- 设置分页及左右两边的箭头 -->
          <div class="swiper-pagination" slot="pagination"></div>
          <div class="swiper-button-prev" slot="button-prev"></div>
          <div class="swiper-button-next" slot="button-next"></div>
        </swiper>
      </div>
      <!-- 轮播 end -->

      <!-- 广告位 start-->
      <div class="ads-box">
        <a
          :href="'/#/product/' + item.id"
          v-for="(item, index) in adsList"
          :key="index"
        >
          <img :src="item.img" alt="" />
        </a>
      </div>
      <!-- 广告位 end-->

      <!-- 横幅广告 start-->
      <div class="banner">
        <a href="/#/product/30">
          <img src="../../public/imgs/banner-1.png" alt="" />
        </a>
      </div>
      <!-- 横幅广告 end-->
    </div>
    <!-- 商品列表 start -->
    <div class="product-box">
      <div class="container">
        <h2>手机</h2>
        <div class="wrapper">
          <div class="banner-left">
            <a href="/#/product/35">
              <img src="../../public/imgs/mix-alpha.jpg" alt="" />
            </a>
          </div>
          <div class="list-box">
            <div class="list" v-for="(arr, i) in phoneList" :key="i">
              <div class="item" v-for="(item, j) in arr" :key="j">
                <span :class="{ 'new-pro': j % 2 == 0 }">新品</span>
                <div class="item-img">
                  <img :src="item.mainImage" alt="" />
                </div>
                <div class="item-info">
                  <h3>{{ item.name }}</h3>
                </div>
                <p>{{ item.subtitle }}</p>
                <p class="price">{{ item.price }}元</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 商品列表 end -->
    <service-bar></service-bar>
  </div>
</template>

<script>
import ServiceBar from "../components/ServiceBar.vue";
// 导入轮播图插件
import { swiper, swiperSlide } from "vue-awesome-swiper";
// // 其实当前目录下没有，但是它自己会去node module模块里面查找
import "swiper/dist/css/swiper.css";
export default {
  name: "index",

  data() {
    return {
      // 轮播图的选项
      swiperOption: {
        // 设置循环，自动播放
        autoplay: true,
        // 设置循环点击（否则当点到最后一张就点不动了）
        loop: true,
        // 设置动画
        effect: "cube",
        // 设置动画的一些参数
        cubeEffect: {
          slideShadows: true,
          shadow: true,
          shadowOffset: 100,
          shadowScale: 0.6,
        },
        // 指定分页器是哪一个
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
        // 设置前进后退的按钮（可点击跳转）
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      },
      // 头部下拉框的列表
      sliderList: [
        {
          id: "42",
          img: require("../../public/imgs/slider/slide-1.jpg"),
        },
        {
          id: "45",
          img: require("../../public/imgs/slider/slide-2.jpg"),
        },
        {
          id: "46",
          img: require("../../public/imgs/slider/slide-3.jpg"),
        },
      ],
      // 左侧导航栏的列表
      menuList: [
        [
          {
            id: 30,
            img: require("../../public/imgs/item-box-1.png"),
            name: "小米CC9",
          },
          {
            id: 31,
            img: require("../../public/imgs/item-box-2.png"),

            name: "小米8青春版",
          },
          {
            id: 32,
            img: require("../../public/imgs/item-box-3.jpg"),

            name: "Redmi k20 Pro",
          },
          {
            id: 34,
            img: require("../../public/imgs/item-box-4.jpg"),

            name: "移动4G专区",
          },
        ],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
      ],
      // 广告位列表
      adsList: [
        { id: 33, img: require("../../public/imgs/ads/ads-1.png") },
        { id: 48, img: require("../../public/imgs/ads/ads-2.jpg") },
        { id: 45, img: require("../../public/imgs/ads/ads-3.png") },
        { id: 47, img: require("../../public/imgs/ads/ads-4.jpg") },
      ],
      // 商品列表
      phoneList: [],
    };
  },
  // 用于初始化
  mounted() {
    this.getProductList();
  },
  methods: {
    getProductList() {
      this.axios
        .get("/products", {
          params: { categoryId: 100012, pageSize: 14 },
        })
        .then((res) => {
          // 将数组转换成二维数组
          this.phoneList = [res.list.slice(6, 10), res.list.slice(10, 14)];
        });
    },
  },
  components: {
    swiper,
    swiperSlide,
    ServiceBar,
  },
};
</script>
<style lang="scss" scoped>
@import "../assets/scss/mixin.scss";
@import "../assets/scss/config.scss";

.index {
  .swiper-box {
    position: relative;
    .swiper-container {
      height: 451px;
      img {
        width: 100%;
      }
      .swiper-button-prev {
        left: 274px;
      }
    }
    .nav-menu {
      position: absolute;
      z-index: 9;
      width: 264px;
      height: 451px;
      padding: 26px 0;
      background-color: #55585a8c;
      box-sizing: border-box;
      // opacity: 0.7; 会把所有的元素，包括字体都会有透明
      .menu-wrap {
        .menu-item {
          height: 50px;
          line-height: 50px;
          a {
            display: block;
            position: relative;
            font-size: 16px;
            color: #fff;
            padding-left: 30px;
            &:after {
              position: absolute;
              right: 30px;
              top: 17.5px;
              content: "";
              @include bgImg(10px, 15px, "../../public/imgs/icon-arrow.png");
            }
          }
          &:hover {
            background-color: #ff6600;
            .children {
              display: block;
            }
          }
          .children {
            display: none;
            position: absolute;
            left: 264px;
            top: 0;
            border: 1px solid #e5e5e5;
            width: 962px;
            height: 451px;
            background-color: #fff;
            ul {
              display: flex;
              justify-content: space-between;
              height: 75px;
              li {
                height: 75px;
                line-height: 75px;
                width: 241px;
                padding-left: 23px;

                a {
                  color: #333;
                  font-size: 14px;
                }
                img {
                  width: 35px;
                  height: 42px;
                  vertical-align: middle;
                  margin-right: 15px;
                }
              }
            }
          }
        }
      }
    }
  }
  .ads-box {
    @include flex();
    margin-top: 13px;
    a {
      width: 296px;
      height: 167px;
    }
  }
  .banner {
    margin-top: 30px;
    margin-bottom: 50px;
  }
  .product-box {
    background-color: $colorJ;
    padding: 30px 0;
    .container {
      h2 {
        font-size: 22px;
        height: 21px;
        line-height: 21px;
        color: $colorB;
        margin-bottom: 20px;
      }
      .wrapper {
        display: flex;
        .banner-left {
          margin-right: 16px;
          img {
            width: 224px;
            height: 619px;
          }
        }
        .list-box {
          .list {
            @include flex();
            width: 986px;
            margin-bottom: 14px;
            &:last-child {
              margin-bottom: 0;
            }
            .item {
              width: 236px;
              height: 302px;
              background-color: $colorG;
              text-align: center;
              span {
                display: inline-block;
                margin: 0 auto;
                width: 67px;
                height: 24px;
                font-size: 14px;
                line-height: 24px;
                color: #fff;
                &.new-pro {
                  background-color: #7ecf68;
                }
                &.kill-pro {
                  background-color: #e82626;
                }
              }
              .item-img {
                img {
                  height: 195px;
                  width: 100%;
                }
              }
              .item-info {
                h3 {
                  font-size: 14px;
                  color: $colorB;
                  line-height: 14px;
                  font-weight: 700;
                }
              }
              p {
                color: $colorD;
                line-height: 13px;
                margin: 6px auto 13px;
              }
              .price {
                font-size: 14px;
                color: #ff6600;
                font-weight: 700;
                cursor: pointer;
                &:after {
                  content: "";
                  @include bgImg(
                    22px,
                    22px,
                    "../../public/imgs/icon-cart-hover.png"
                  );
                  vertical-align: middle;
                  margin-left: 5px;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>




